package spring.cloud.tasks.tasks_manager.listener;


import spring.cloud.tasks.common.curator.Curator;
import spring.cloud.tasks.common.task_item_allot.Executor;
import spring.cloud.tasks.common.task_item_allot.TaskItem;
import spring.cloud.tasks.tasks_manager.schedule.ScheduleManager;
import spring.cloud.tasks.tasks_manager.task_item_assign.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ExecutorOfflineOrFollowControlTaskItemAssign {

    /**
     * @param scheduleManager
     * @param executorId
     * @param offlineOrFollowControl 下线为true,流控为false
     * @throws Exception
     */
    public void taskItemAssign(ScheduleManager scheduleManager, String executorId, boolean offlineOrFollowControl) throws Exception {
        Curator curator = scheduleManager.curator;
        TaskItemContainer taskItemContainer = scheduleManager.taskItemContainer;
        ExecutorTaskItemListContext assignedExecutorTaskItemListContext = taskItemContainer.assignedExecutorTaskItemListContext;
        PreferExecutorSettings preferExecutorSettings = assignedExecutorTaskItemListContext.preferExecutorSettings;
        if (preferExecutorSettings == null) {
            return;//如果这个参数没有则代表还没有进行预分配,那么就不进行后续操作了。
        }
        /**
         * 正在调度分配的任务分片容器
         */
        ExecutorTaskItemListContext assigningExecutorTaskItemListContext = new ExecutorTaskItemListContext();
        List<Executor> onlineExecutorList = assigningExecutorTaskItemListContext.getExecutorList();
        List<Executor> oldOnlineExecutorList = assignedExecutorTaskItemListContext.getExecutorList();
        onlineExecutorList.addAll(TaskItemAssignUtils.copyExecutorList(oldOnlineExecutorList));
        List<TaskItem> pickUpEdTaskItemList = new ArrayList<>();
        Iterator<Executor> iterator = onlineExecutorList.iterator();
        while (iterator.hasNext()) {
            Executor executor = iterator.next();
            if (!executorId.equals(executor.getExecutorId())) {
                continue;
            }
            List<TaskItem> taskItemList = executor.getTaskItemList();
            for (TaskItem taskItem : taskItemList) {
                pickUpEdTaskItemList.add(taskItem);
            }
            executor.getTaskItemList().clear();
            if (offlineOrFollowControl) {
                iterator.remove();
            }
        }
        boolean pickUpEd = pickUpEdTaskItemList.size() > 0;
        //获取当前镜像的enableTaskIdList
        List<String> enableTaskIdList = assignedExecutorTaskItemListContext.enableTaskIdList;
        if (pickUpEd) {
            // 放回
            TaskItemAssign.putBack(pickUpEdTaskItemList, onlineExecutorList, preferExecutorSettings
            );
            // 持久化
            List<String> executorIdList = new ArrayList<>();
            for (Executor executor : assigningExecutorTaskItemListContext.getExecutorList()) {
                executorIdList.add(executor.getExecutorId());
            }
            assigningExecutorTaskItemListContext.setExecutorIdList(executorIdList);
            taskItemContainer.assignedExecutorTaskItemListContext = assigningExecutorTaskItemListContext;
            assignedExecutorTaskItemListContext.getExecutorList().clear();
            assignedExecutorTaskItemListContext = null;

        }
    }
}
